﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace homework
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            double  a = 0, b = 0, e_2 = 0, e_dot_2 = 0,B2,L2,A21,rou=206264.806247096355;
            int pp = 0;
            if (radioButton1.Checked)
            {
                a = 6378245;//椭圆长半轴
                b = 6356863.0187730473;//椭圆短半轴
                e_2 = 0.006693421622966;//椭圆第一偏心率e^2
                e_dot_2 = 0.006738525414683;//椭圆第二偏心率e'^2 
                pp = 1;
            }
            else if (radioButton2.Checked)//
            {
                a = 6378140;//椭圆长半轴
                b = 6356755.2881575287;//椭圆短半轴
                e_2 = 0.006694384999588;//椭圆第一偏心率e^2
                e_dot_2 = 0.006739501819473;//椭圆第二偏心率e'^2 
                pp = 1;
            }
            else if (radioButton3.Checked)//
            {
                a = 6378137;//椭圆长半轴
                b = 6356752.3142;//椭圆短半轴
                e_2 = 0.00669437999013;//椭圆第一偏心率e^2
                e_dot_2 = 0.00673949674227;//椭圆第二偏心率e'^2 
                pp = 1;
            }
            else if (radioButton4.Checked)//
            {
                a = 6378137;//椭圆长半轴
                b = 6356752.3141;//椭圆短半轴
                e_2 = 0.00669438002290;//椭圆第一偏心率e^2
                e_dot_2 = 0.00673949677548;//椭圆第二偏心率e'^2 
                pp = 1;
            }
            else if (pp == 0)
            {
                MessageBox.Show("请选择椭球体", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            double c = a * a / b;
            //转化为弧度制
            double B1 = (Convert.ToDouble(textBox1.Text)+ Convert.ToDouble(textBox2.Text) /60+ Convert.ToDouble(textBox3.Text) /3600)/180*Math.PI;
            double L1= (Convert.ToDouble(textBox4.Text) + Convert.ToDouble(textBox5.Text) / 60 + Convert.ToDouble(textBox6.Text) / 3600) / 180 * Math.PI;
            double A1= (Convert.ToDouble(textBox16.Text) + Convert.ToDouble(textBox17.Text) / 60 + Convert.ToDouble(textBox18.Text) / 3600) / 180 * Math.PI;
            double S = Convert.ToDouble(textBox19.Text);

            double W = Math.Sqrt(1 - e_2 * Math.Sin(B1 ) * Math.Sin(B1));
            double V = Math.Sqrt(1 + e_dot_2 * Math.Cos(B1 ) * Math.Cos(B1 ));
            double M1 = a * (1 - e_2) / (W * W * W);
            double N1 = a / W;

            double t1 = Math.Tan(B1);
            //单位秒，循环迭代初始值
            double delta_B = V * V / N1 * rou * S * Math.Cos(A1), delta_L = rou / N1 * S / Math.Cos(B1) * Math.Sin(A1), delta_A = rou / N1 * S * Math.Sin(A1) * t1;
            double chB, chL, chA;
            double Bm = B1 + delta_B / rou / 2, Am = A1 + delta_A / rou / 2;//单位为弧度制
            double tm = Math.Tan(Bm), nm = e_dot_2 * Math.Cos(Bm) * Math.Cos(Bm);
            double Wm = Math.Sqrt(1 - e_2 * Math.Sin(Bm) * Math.Sin(Bm));
            double Vm = Math.Sqrt(1 + e_dot_2 * Math.Cos(Bm) * Math.Cos(Bm));
            double Mm = a * (1 - e_2) / (Wm * Wm * Wm);
            double Nm = a / Wm;
            do//循环
            {
                chB = delta_B;
                chL = delta_L;
                chA = delta_A;
                delta_B = Vm * Vm/ Nm * rou * S * Math.Cos(Am) * (1 + S * S / 24 / Nm / Nm * (Math.Pow(Math.Sin(2 + 3 * tm * tm + 3 * Math.Pow(nm * tm, 2)), 2)) + 3 *
                    nm * nm * Math.Cos(Am) * (tm * tm - 1 - nm * nm - 4 * tm * tm * nm * nm));
                delta_L = rou / Nm * S / Math.Cos(Bm) * Math.Sin(Am) * (1 + S * S / 24 / Nm / Nm * (Math.Sin(Am) * Math.Sin(Am) * tm * tm - Math.Cos(Am) * Math.Cos(Am) *
                    (1 + nm * nm - 9 * tm * tm * nm * nm + Math.Pow(nm, 4))));
                delta_A = rou / Nm * S * Math.Sin(Am) * tm * (1 + S * S / 24 / Nm / Nm * (Math.Cos(Am) * Math.Cos(Am) * (2 + 7 * nm * nm + 9 * tm * tm * nm * nm + 5 * Math.Pow(nm, 4))
                    + Math.Sin(Am) * Math.Sin(Am) * (2 + tm * tm + 2 * nm * nm)));
                Bm = B1 + delta_B / rou / 2;
                Am = A1 + delta_A / rou / 2;
                Wm = Math.Sqrt(1 - e_2 * Math.Sin(Bm) * Math.Sin(Bm));
                Vm = Math.Sqrt(1 + e_dot_2 * Math.Cos(Bm) * Math.Cos(Bm));
                Mm = a * (1 - e_2) / (Wm * Wm * Wm);
                Nm = a / Wm;
                tm = Math.Tan(Bm);
                nm = e_dot_2 * Math.Cos(Bm) * Math.Cos(Bm);
            } while (Math.Abs(chB-delta_B)>1e-4|| Math.Abs(chA - delta_A) > 1e-4|| Math.Abs(chL - delta_L) > 1e-4);

            B2 = (B1 + delta_B / rou) * 180 / Math.PI;
            L2= (L1 + delta_L/ rou) * 180 / Math.PI;
            A21 = (A1 + delta_A / rou) * 180 / Math.PI;
            if (A1 > 180) A21 = A21 + 180;
            else  A21 = A21 - 180;
            if (A21 < 0) A21 += 360;
            //打印纬度
            textBox7.Text = Convert.ToString(Math.Floor(B2));
            textBox8.Text = Convert.ToString(Math.Floor((B2 - Math.Floor(B2)) * 60));
            textBox9.Text = Convert.ToString(((B2 - Math.Floor(B2)) * 60 - Math.Floor((B2 - Math.Floor(B2)) * 60)) * 60);
            //打印经度
            textBox10.Text = Convert.ToString(Math.Floor(L2));
            textBox11.Text = Convert.ToString(Math.Floor((L2 - Math.Floor(L2)) * 60));
            textBox12.Text = Convert.ToString(((L2 - Math.Floor(L2)) * 60 - Math.Floor((L2 - Math.Floor(L2)) * 60)) * 60);
            //打印A21
            textBox13.Text = Convert.ToString(Math.Floor(A21));
            textBox14.Text = Convert.ToString(Math.Floor((A21 - Math.Floor(A21)) * 60));
            textBox15.Text = Convert.ToString(((A21 - Math.Floor(A21)) * 60 - Math.Floor((A21 - Math.Floor(A21)) * 60)) * 60);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = string.Empty;
            textBox2.Text = string.Empty;
            textBox3.Text = string.Empty;
            textBox4.Text = string.Empty;
            textBox5.Text = string.Empty;
            textBox6.Text = string.Empty;
            textBox7.Text = string.Empty;
            textBox8.Text = string.Empty;
            textBox9.Text = string.Empty;
            textBox10.Text = string.Empty;
            textBox11.Text = string.Empty;
            textBox12.Text = string.Empty;
            textBox13.Text = string.Empty;
            textBox14.Text = string.Empty;
            textBox15.Text = string.Empty;
            textBox16.Text = string.Empty;
            textBox17.Text = string.Empty;
            textBox18.Text = string.Empty;
            textBox19.Text = string.Empty;
        }
    }
}
